package cn.org.mrliu.ssm.service.impl;


import javax.annotation.Resource;

import cn.org.mrliu.ssm.util.Pagination;
import com.github.pagehelper.PageHelper;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.org.mrliu.ssm.common.mapper.BaseMapper;
import cn.org.mrliu.ssm.common.service.impl.BaseServiceSupport;
import cn.org.mrliu.ssm.mapper.CustomerMapper;
import cn.org.mrliu.ssm.model.Customer;
import cn.org.mrliu.ssm.service.CustomerService;

@Service
public class CustomerServiceImpl extends BaseServiceSupport<Customer> implements CustomerService{
	@Resource
	private CustomerMapper customerMapper;

	@SuppressWarnings("rawtypes")
	@Override
	protected BaseMapper getBaseMapper() {
		return customerMapper;
	}

	@Override
	@Cacheable("redisCache-2m")
	public Pagination getCustomerList(Integer pageIndex, Integer pageSize, String hello) throws Exception {
		System.out.println("getCustomerList.....");
		PageHelper.startPage(pageIndex, pageSize);
		return new Pagination(getBaseMapper().getPageList(null));
	}

	@Override
	public void addCustomer(Customer customer) throws Exception {
		System.out.println("addCustomer.......");
		customerMapper.save(customer);
		
	}
	
	/**
	 * @CacheEvict 清除缓存,默认不清除所有缓存
	 *   如果设置:allEntries=true,表示清除当前组的所有缓存
	 *   keyGenerator:指定key的生成策略
	 *   
	 *   以下代码表示,指定key的生成策略为updateCustomerKeyGenerator,然后将修改的对象从缓存中删除
	 *   @CacheEvict(value="customerDetailCache",keyGenerator="updateCustomerKeyGenerator")
	 */
//	@CacheEvict(value="customerDetailCache",keyGenerator="updateCustomerKeyGenerator")
	@Override
	public void updateCustomer(Customer customer) throws Exception {
		System.out.println("updateCustomer.....");
		customerMapper.update(customer);
	}

	/**
	 * @CacheEvict 删除缓存
	 *  allEntries=true 删除当前key所有的缓存数据
	 *  beforeInvocation=true 在调用该方法之前就删除该key对应的所有缓存数据,根据情况设置
	 */
	@CacheEvict(value="customerDetailCache",allEntries=true,beforeInvocation=true)
	@Override
	public void deleteCustomer(Integer id) throws Exception {
		System.out.println("deleteCustomer......");
		customerMapper.delete(id);
	}
	
	@Cacheable(value="customerDetailCache")
	@Override
	public Customer getCustomer(Integer id) throws Exception {
		System.out.println("getCustomer.........");
		return customerMapper.get(id);
	}
	
	
	public void deleteCustomerCache(){
		System.out.println("deleteCustomerCache...........");
	}

	@Override
	public Customer getCustomer1(Integer id) throws Exception {
		return getCustomer(id);
	}
	
}
